import tkinter as tk
from tkcalendar import DateEntry


# pip install tkcalendar

class LoanInterestCalculator:
    def __init__(self, root):
        self.root = root
        self.root.title("借款利息计算器")

        # 设置GUI界面
        self.principal_label = tk.Label(root, text="本金：")
        self.principal_label.grid(row=0, column=0, padx=5, pady=5, sticky=tk.W)

        self.principal_entry = tk.Entry(root)
        self.principal_entry.grid(row=0, column=1, padx=5, pady=5, sticky=tk.E)

        self.borrow_date_label = tk.Label(root, text="借款日期：")
        self.borrow_date_label.grid(row=1, column=0, padx=5, pady=5, sticky=tk.W)

        self.borrow_date_entry = DateEntry(root, locale='zh_CN', date_pattern='yyyy-mm-dd')
        self.borrow_date_entry.grid(row=1, column=1, padx=5, pady=5, sticky=tk.E)

        self.return_date_label = tk.Label(root, text="还款日期：")
        self.return_date_label.grid(row=2, column=0, padx=5, pady=5, sticky=tk.W)

        self.return_date_entry = DateEntry(root, locale='zh_CN', date_pattern='yyyy-mm-dd')
        self.return_date_entry.grid(row=2, column=1, padx=5, pady=5, sticky=tk.E)

        self.interest_rate_label = tk.Label(root, text="年利率（%）：")
        self.interest_rate_label.grid(row=3, column=0, padx=5, pady=5, sticky=tk.W)

        self.interest_rate_entry = tk.Entry(root)
        self.interest_rate_entry.insert(0, "24")
        self.interest_rate_entry.grid(row=3, column=1, padx=5, pady=5, sticky=tk.E)

        self.calculate_button = tk.Button(root, text="计算利息", command=self.calculate_interest)
        self.calculate_button.grid(row=4, column=0, padx=5, pady=5, sticky=tk.W)

        self.interest_label = tk.Label(root, text="利息：")
        self.interest_label.grid(row=5, column=0, padx=5, pady=5, sticky=tk.W)

        self.interest_result_label = tk.Label(root, text="")
        self.interest_result_label.grid(row=5, column=1, padx=5, pady=5, sticky=tk.E)

        self.total_label = tk.Label(root, text="本金+利息：")
        self.total_label.grid(row=6, column=0, padx=5, pady=5, sticky=tk.W)

        self.total_result_label = tk.Label(root, text="")
        self.total_result_label.grid(row=6, column=1, padx=5, pady=5, sticky=tk.E)

        self.days_label = tk.Label(root, text="累计天数：")
        self.days_label.grid(row=7, column=0, padx=5, pady=5, sticky=tk.W)

        self.days_result_label = tk.Label(root, text="")
        self.days_result_label.grid(row=7, column=1, padx=5, pady=5, sticky=tk.E)

    def calculate_interest(self):
        # 获取输入数据
        principal = float(self.principal_entry.get())
        borrow_date = self.borrow_date_entry.get()
        return_date = self.return_date_entry.get()
        interest_rate = float(self.interest_rate_entry.get()) / 100

        # 计算利息
        from datetime import datetime
        borrow_date = datetime.strptime(borrow_date, "%Y-%m-%d")
        return_date = datetime.strptime(return_date, "%Y-%m-%d")
        days = (return_date - borrow_date).days
        daily_rate = interest_rate / 365.0
        interest = principal * daily_rate * days

        # 更新界面
        self.days_result_label.config(text="{} 天".format(days))
        self.interest_result_label.config(text="{:.2f} 元".format(interest))
        self.total_result_label.config(text="{:.2f} 元".format(principal + interest))


if __name__ == "__main__":
    root = tk.Tk()
    app = LoanInterestCalculator(root)
    root.mainloop()
